<?php
class Block_BlkCategoryMenu extends Zend_View_Helper_Abstract{
    
    public function blkCategoryMenu() {
        $view = $this->view;
        $arrParam = $view->arrParam;
        
        $frontend = "Core";
        $backend = "File";
        $frontendOptions = array('cat_id_prefix'=>'myCache_', 'lifetime'=>900);
        $backendOptions = array('cache_dir'=>CACHE_PATH . '/block');
        $cache = Zend_Cache::factory($frontend, $backend, 
                                        $frontendOptions, $backendOptions);

//        if(!$strMenu = $cache->load('menu_left')){
            $db = Zend_Registry::get('connectDb');
//          $db = Zend_Db::factory($adapter, $config);
            $select = $db->select()
                     ->from('product_category', array('id', 'name', 'parents'))
                     ->where('status = ?' , 1)
                     ->order('order ASC');
        
            $result = $db->fetchAll($select);
            $strMenu = $this->createMenu($result, 0, $view);
            $cache->save($strMenu, 'menu_left');
//        }
        
        $cid = $arrParam['cid'];
        if($arrParam['module'] == 'shopping' && $cid > 0){
            $system = new Zendvn_System_Recursive($result);
            $parents = $system->getParentsIdArray($cid, array('type'=>1));
            $arrParents = Zend_Json::encode($parents);
        }
        //http://localhost/zfo/shopping/index/category/id/4/name/
        
        require_once (BLOCK_PATH . '/BlkCategoryMenu/default.php');
    }
    
    public function createMenu($sourceArr, $parents = 0, $viewObj) {
        $this->recursiveMenu($sourceArr, $parents, $newMenu, $viewObj);
        return str_replace('<ul></ul>', '', $newMenu);
    }
    
    public function recursiveMenu($sourceArr,$parents = 0,&$newMenu, $viewObj){
		$filter = new Zend_Filter();
		$multiFilter = $filter->addFilter(new Zendvn_Filter_RemoveCircumflex())
                                        ->addFilter(new Zend_Filter_StringToLower(array('encoding'=>'UTF-8')))
                                        ->addFilter(new Zend_Filter_Alnum(true))
                                        ->addFilter(new Zend_Filter_PregReplace(array('match'=>'#\s+#','replace'=>'-')))
                                        ->addFilter(new Zend_Filter_Word_SeparatorToDash());
		if(count($sourceArr)>0){
			$idUL = 'ulGroup_' . $parents;
			$newMenu .= '<ul id="' .$idUL .'">';
			foreach ($sourceArr as $key => $value){
				if($value['parents'] == $parents){
					$liMenu = 'liMenu_' . $value['id'];
					if($value['parents'] == 0){
						$newMenu .= '<li id="' .$liMenu .'"><a>' . $value['name'] . '</a>';
					}else{
                                            $urlOptions = array('module'=>'shopping', 'controller'=>'index', 'action'=>'category', 
                                                'cid'=>$value['id'], 'name'=>$multiFilter->filter($value['name']));
                                            $link = $viewObj->url($urlOptions, 'shop-category');
						$newMenu .= '<li id="' .$liMenu .'"><a href="' . $link . '" id="' .$liMenu .'">' . $value['name'] . '</a>';												 
					}					
					
					$newParents = $value['id'];
					unset($sourceArr[$key]);
					$this->recursiveMenu($sourceArr,$newParents, $newMenu, $viewObj);
					$newMenu .= '</li>';
				}
			}
			$newMenu .= '</ul>';
		}
	}
}